package com.hjm.project.AgentMarketingReward.service.impl;

import java.text.ParseException;
import java.util.List;

import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.StringUtils;
import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.project.agentInfo.agentInfo.domain.AgentInfo;
import com.hjm.project.agentInfo.agentInfo.mapper.AgentInfoMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import com.hjm.project.AgentMarketingReward.mapper.AgentMarketingRewardMapper;
import com.hjm.project.AgentMarketingReward.domain.AgentMarketingReward;
import com.hjm.project.AgentMarketingReward.service.IAgentMarketingRewardService;
import com.hjm.common.utils.text.Convert;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 资深总监营销奖励Service业务层处理
 * 
 * @author maker-platform
 * @date 2021-04-06
 */
@Service
public class AgentMarketingRewardServiceImpl implements IAgentMarketingRewardService 
{
    @Resource
    private AgentMarketingRewardMapper agentMarketingRewardMapper;
    @Resource
    private AgentInfoMapper agentInfoMapper;

    /**
     * 查询资深总监营销奖励
     * 
     * @param id 资深总监营销奖励ID
     * @return 资深总监营销奖励
     */
    @Override
    public AgentMarketingReward selectAgentMarketingRewardById(Integer id)
    {
        return agentMarketingRewardMapper.selectAgentMarketingRewardById(id);
    }

    /**
     * 查询资深总监营销奖励列表
     * 
     * @param agentMarketingReward 资深总监营销奖励
     * @return 资深总监营销奖励
     */
    @Override
    public List<AgentMarketingReward> selectAgentMarketingRewardList(AgentMarketingReward agentMarketingReward)
    {
        return agentMarketingRewardMapper.selectAgentMarketingRewardList(agentMarketingReward);
    }

    /**
     * 新增资深总监营销奖励
     * 
     * @param agentMarketingReward 资深总监营销奖励
     * @return 结果
     */
    @Override
    public int insertAgentMarketingReward(AgentMarketingReward agentMarketingReward)
    {
        return agentMarketingRewardMapper.insertAgentMarketingReward(agentMarketingReward);
    }

    /**
     * 修改资深总监营销奖励
     * 
     * @param agentMarketingReward 资深总监营销奖励
     * @return 结果
     */
    @Override
    @Transactional
    public int updateAgentMarketingReward(AgentMarketingReward agentMarketingReward) {
        if (agentMarketingReward.getStatus().equals("1")) {
            AgentMarketingReward marketingReward = new AgentMarketingReward();
            marketingReward.setAgentNum(agentMarketingReward.getAgentNum());
            marketingReward.setStatus("1");
            List<AgentMarketingReward> agentMarketingRewards = agentMarketingRewardMapper.selectAgentMarketingRewardList(marketingReward);
            if (CollectionUtils.isNotEmpty(agentMarketingRewards) && agentMarketingRewards.size() >= 1) {
                throw  new BusinessException("服务商奖励只可启用一个,请先禁用其他奖励");
            }
        }
        return agentMarketingRewardMapper.updateAgentMarketingReward(agentMarketingReward);
    }

    /**
     * 删除资深总监营销奖励对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAgentMarketingRewardByIds(String ids)
    {
        return agentMarketingRewardMapper.deleteAgentMarketingRewardByIds(Convert.toStrArray(ids));
    }



    /**
     * 添加达标返现数据
     * @Param [agentMarketingReward]
     * @Return int
     */
    @Transactional
    public int addReward(AgentMarketingReward agentMarketingReward) throws ParseException {
        //查看是否存在重复数据,一个V9（营销总监只能给他的下级不同服务商设置一条奖励,如果重复设置,必须要禁用其他保留一条）
        AgentMarketingReward reward = new AgentMarketingReward();
        if (StringUtils.isEmpty(agentMarketingReward.getAgentNum())) {
            reward.setAgentNum(agentMarketingReward.getTopAgentNum());
        } else {
            reward.setAgentNum(agentMarketingReward.getAgentNum());
        }
        reward.setStatus("1");
        List<AgentMarketingReward> agentMarketingRewards = agentMarketingRewardMapper.selectAgentMarketingRewardList(reward);
        if (CollectionUtils.isNotEmpty(agentMarketingRewards)) {
            throw new BusinessException("目标代理已设置达标奖励,请先禁用");
        }

        String date = null;
        if (agentMarketingReward.getEndDate().equals("1")) {           //1个月
            date = DateUtils.getDateMonth(DateUtils.YYYY_MM_DD, 1);
        } else if(agentMarketingReward.getEndDate().equals("3")) {     //6个月
            date = DateUtils.getDateMonth(DateUtils.YYYY_MM_DD, 3);
        } else if(agentMarketingReward.getEndDate().equals("6")) {     //6个月
            date = DateUtils.getDateMonth(DateUtils.YYYY_MM_DD, 6);
        } else if(agentMarketingReward.getEndDate().equals("9")) {     //9个月
            date = DateUtils.getDateMonth(DateUtils.YYYY_MM_DD, 9);
        } else if(agentMarketingReward.getEndDate().equals("12")) {    //1年
            date = DateUtils.getYear(DateUtils.YYYY_MM_DD, 1);
        }
        if (StringUtils.isNotEmpty(agentMarketingReward.getFxEight()) && agentMarketingReward.getFxEight().equals("1")) {
            agentMarketingReward.setFxEight("8");
        } else {
            agentMarketingReward.setFxEight("0");
        }

        //如果不选择返现对象默认反自己
        if (StringUtils.isEmpty(agentMarketingReward.getAgentNum())) {
            AgentInfo agentInfo = new AgentInfo();
            agentInfo.setAgentNum(agentMarketingReward.getTopAgentNum());
            AgentInfo agentInfos = agentInfoMapper.selectAgentInfoList(agentInfo).get(0);
            //获取上级信息
            agentInfo = new AgentInfo();
            agentInfo.setAgentNum(agentInfos.getAgentNum());
            AgentInfo topAgent = agentInfoMapper.selectAgentInfoList(agentInfo).get(0);
            agentMarketingReward.setOnAgentNum(topAgent.getAgentNum());
            agentMarketingReward.setOnAgentName(topAgent.getAgentName());
            agentMarketingReward.setAgentNum(agentMarketingReward.getTopAgentNum());
            agentMarketingReward.setAgentName(agentMarketingReward.getTopAgentName());
        }


        agentMarketingReward.setStatus("1");
        agentMarketingReward.setAddMan(ShiroUtils.getSysUser().getLoginName());
        agentMarketingReward.setAddDate(DateUtils.getDateTime());
        //agentMarketingReward.setFxYsf(agentMarketingReward.getFxYsf() == null ? "0" : "0.0002");
        agentMarketingReward.setEndDate(date);
        int i = agentMarketingRewardMapper.insertAgentMarketingReward(agentMarketingReward);
        if (i <= 0) {
            throw new BusinessException("添加奖励失败,请重试");
        }
        return i;
    }

    /**
     * 删除资深总监营销奖励信息
     * 
     * @param id 资深总监营销奖励ID
     * @return 结果
     */
    @Override
    public int deleteAgentMarketingRewardById(Integer id)
    {
        return agentMarketingRewardMapper.deleteAgentMarketingRewardById(id);
    }
}
